package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcCallOutDoctorRecordMapper;
import com.csun.cmny.provider.model.constant.OmcConstant;
import com.csun.cmny.provider.model.domain.OmcCallOutDoctorRecord;
import com.csun.cmny.provider.model.domain.OmcEverydayDisinfectionRecord;
import com.csun.cmny.provider.model.dto.callOutDoctorRecord.CallOutDoctorRecordSaveDto;
import com.csun.cmny.provider.model.dto.callOutDoctorRecord.CallOutDoctorRecordSelectDto;
import com.csun.cmny.provider.model.vo.callOutDoctorRecord.CallOutDoctorRecordVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcCallOutDoctorRecordService;
import com.csun.cmny.provider.utils.JPushService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class OmcCallOutDoctorRecordServiceImpl extends BaseService<OmcCallOutDoctorRecord> implements OmcCallOutDoctorRecordService {

    @Resource
    private JPushService jpushService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcCallOutDoctorRecordMapper callOutDoctorRecordMapper;

    @Override
    public int callDoctor(CallOutDoctorRecordSaveDto callOutDoctorRecordSaveDto) {

        OmcCallOutDoctorRecord omcCallOutDoctorRecord = new OmcCallOutDoctorRecord();

        BeanUtils.copyProperties(callOutDoctorRecordSaveDto,omcCallOutDoctorRecord);

        int save = save(omcCallOutDoctorRecord);

        //推送给医生
        String message = JSONObject.fromObject(omcCallOutDoctorRecord).toString();

        List<String> tagListDoctorUser = Lists.newArrayList();

        tagListDoctorUser.add(OmcConstant.USER_PREFIX+callOutDoctorRecordSaveDto.getDoctorUserId());

        String[] oldName = {callOutDoctorRecordSaveDto.getOldManName()};

        //呼叫推送给医生
        jpushService.sendToHualongDoctor(cmnyMessageSource.getMessage("call.out.doctor", oldName),
                cmnyMessageSource.getMessage("call.out.doctor", oldName),
                message,
                message,
                tagListDoctorUser);

        return save;
    }

    @Override
    public List<CallOutDoctorRecordVo> selectCallOutDoctorRecord(CallOutDoctorRecordSelectDto callOutDoctorRecordSelectDto) {
        return callOutDoctorRecordMapper.selectCallOutDoctorRecord(callOutDoctorRecordSelectDto);
    }
}
